/**
 * Copyright(c) 2025 hbswxx.com, All Rights Reserved. Author: kouqiang Create date: 2025/5/29
 */
package cn.sw.syncdata.config.headerinfo;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * description: 远程数据源
 *
 * @author kouqiang
 * @date 2025/5/29 13:33
 */
@Configuration
@MapperScan(basePackages = "cn.sw.syncdata.remote.mapper",sqlSessionFactoryRef = "sqlRemoteSessionFactory")
public class RemoteDataBaseHeaderConfig {
    @Bean
    @ConfigurationProperties("spring.datasource.remote")
    public DataSource dataSourceRemote(){
        return DataSourceBuilder.create().build();
    }
    @Bean
    public SqlSessionFactory sqlRemoteSessionFactory(@Qualifier("dataSourceRemote") DataSource dataSourceLocal) throws Exception{
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSourceLocal);
        // 指定 Mapper XML 路径
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/remote/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }
    @Bean
    public DataSourceTransactionManager txManagerRemote(@Qualifier("dataSourceRemote") DataSource dataSourceLocal)
    {
        return new DataSourceTransactionManager(dataSourceLocal);
    }
}
